package cn.game.tanke.panel;

import cn.game.tanke.manger.BulletManager;
import cn.game.tanke.manger.EnemyManager;
import cn.game.tanke.manger.BoomManager;
import cn.game.tanke.role.Boss;
import cn.game.tanke.role.Enemy;
import cn.game.tanke.role.Player;
import cn.game.tanke.util.TankMap;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class TankPanel extends JPanel implements Runnable {
    private Boss boss;
    private Player player;
    private EnemyManager enemyManager;
    private BulletManager bulletManager;
    private BoomManager boomManager;
    private Thread mainThread;
    private boolean isGameOver = false;

    public TankPanel() {
        setBackground(Color.BLACK);
        boss = Boss.getInstance();
        player = Player.getInstance();
        player.setPanel(this); // 设置面板引用

        // 创建敌人管理器并添加敌人
        enemyManager = new EnemyManager(3, this);

        // 创建子弹管理器
        boomManager = new BoomManager();
        bulletManager = new BulletManager(this, boomManager);

        // 添加键盘监听器
        setFocusable(true);
        addKeyListener(player);

        // 启动线程
        mainThread = new Thread(this);
        mainThread.start();
    }

    protected void paintComponent(Graphics g) {
        if (!isGameOver) {
            super.paintComponent(g);
            TankMap.drawMap(g, this);
            boss.drawRole(g, this);
            player.drawRole(g, this);
            enemyManager.drawEnemies(g, this);
            bulletManager.drawBullets(g, this);
            boomManager.drawBooms(g, this);
        } else {
            // 显示游戏结束界面
            g.setColor(Color.WHITE);
            g.setFont(new Font("宋体", Font.BOLD, 36));
            String message = isPlayerWin() ? "玩家胜利!" : "玩家失败!";
            int x = (getWidth() - g.getFontMetrics().stringWidth(message)) / 2;
            int y = getHeight() / 2;
            g.drawString(message, x, y);
        }
    }

    @Override
    public void run() {
        while (!isGameOver) {
            bulletManager.checkCollisions();
            if (enemyManager.areAllEnemiesDead()) {
                gameOver(true);
                stopAllThreads();
            }
            repaint();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public Player getPlayer() {
        return player;
    }

    public List<Enemy> getEnemies() {
        return enemyManager.getEnemies();
    }

    public void gameOver(boolean playerWin) {
        isGameOver = true;
        System.out.println(playerWin ? "玩家胜利!" : "玩家失败!");
        repaint();
    }

    public BulletManager getBulletManager() {
        return bulletManager;
    }

    public void stopAllThreads() {
        mainThread.interrupt();
        enemyManager.stopAllEnemies();
        bulletManager.stopAllBullets();
    }

    private boolean isPlayerWin() {
        return enemyManager.areAllEnemiesDead(); // 假设Boss类有isDead方法
    }
}